﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileMigrateWorker
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = true;
            
        }

        private void btnSrcPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDia = new OpenFileDialog();
            var dia = fileDia.ShowDialog();
            if (dia == DialogResult.OK)
            {
                txtSrc.Text = fileDia.FileName;
            }
        }

        private void btnToPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbDia = new FolderBrowserDialog();
            var dia = fbDia.ShowDialog();
            if (dia == DialogResult.OK)
            {
                string fileName = Path.GetFileName(txtSrc.Text);
                txtTo.Text = Path.Combine(fbDia.SelectedPath, fileName);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.btnPause.Enabled = false;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
        }

        private ES.Migrate.MigrateWorker worker = null;

        private delegate void upDele(int value);
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtSrc.Text))
            {
                MessageBox.Show("源文件路径的文件不存在！", "提示", MessageBoxButtons.OK);
                return;
            }

            worker = new ES.Migrate.MigrateWorker(txtSrc.Text, txtTo.Text, string.Empty);
            this.fileProgressBar.Maximum = 100;
            
            worker.UpdateProgress = (userState,rate) =>
            {
                Console.WriteLine(rate);
                var maxNum = Convert.ToInt32(rate * 100);
                this.fileProgressBar.Invoke(new upDele(UpProgressBar), maxNum);
            };

            worker.WorkerError = (userState, ex) =>
            {
                lblMsg.Text = ex.Message;
                MessageBox.Show(ex.Message);
            };

            worker.WorkerCompleted = (userState, isOk) =>
            {
                MessageBox.Show(isOk.ToString());

                this.fileProgressBar.Value = 0;
                this.btnStart.Enabled = true;
                this.btnPause.Enabled = false;
            };

            this.btnStart.Enabled = false;
            this.btnPause.Enabled = true;
            worker.RunWorkerAsync();
        }

        public void UpProgressBar(int value)
        {
            this.fileProgressBar.Value = value;
        }


        private void btnPause_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = false;
            this.btnContince.Enabled = true;
            this.btnRedo.Enabled = true;
            this.btnCancel.Enabled = true;
            worker.PauseAsync();
        }

        private void btnContince_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
            worker.ContinceAsync();
        }

        private void btnRedo_Click(object sender, EventArgs e)
        {
            this.btnPause.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = false;
            this.btnCancel.Enabled = false;
            worker.RedoAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = true;
            this.btnContince.Enabled = false;
            this.btnRedo.Enabled = true;
            this.btnCancel.Enabled = false;
            worker.CancelAsync();
        }

      
    }
}
